package cn.wolfcode.trip.base.service.impl;

import cn.wolfcode.trip.base.domain.Travel;
import cn.wolfcode.trip.base.domain.TravelContent;
import cn.wolfcode.trip.base.mapper.TravelContentMapper;
import cn.wolfcode.trip.base.mapper.TravelMapper;
import cn.wolfcode.trip.base.query.QueryObject;
import cn.wolfcode.trip.base.query.StrategyTravelQuery;
import cn.wolfcode.trip.base.query.TravelQuery;
import cn.wolfcode.trip.base.service.ITravelService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class TravelServiceImpl implements ITravelService {
    @Autowired
    TravelMapper travelMapper;
    @Autowired
    TravelContentMapper contentMapper;

    @Override
    public PageInfo query(TravelQuery qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize(),qo.getOrderBy());
        List<Travel> list = travelMapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public void save(Travel travel) {
        Date date = new Date();
        //创建时间
        travel.setCreateTime(date);
        //最后更新时间
        travel.setLastUpdateTime(date);
        travelMapper.insert(travel);

        TravelContent travelContent = travel.getTravelContent();
        //设置主键
        travelContent.setId(travel.getId());
        //保存文章内容表
        contentMapper.insert(travelContent);
    }

    @Override
    public Travel getById(Long id) {
        return travelMapper.selectByPrimaryKey(id);
    }

    @Override
    public void update(Travel travel) {
        //最后更新时间
        travel.setLastUpdateTime(new Date());
        travelMapper.updateByPrimaryKey(travel);

        TravelContent travelContent = travel.getTravelContent();
        //设置主键
        travelContent.setId(travel.getId());
        //保存文章内容表
        contentMapper.updateByPrimaryKey(travelContent);
    }

    @Override
    public TravelContent getContentById(Long id) {
        return contentMapper.selectByPrimaryKey(id);
    }

    @Override
    public void changeState(Travel travel) {
        //如果是已发布的状态,就设置发布时间
        if(travel.getState()==Travel.STATE_RELEASE){
            travel.setReleaseTime(new Date());
        }
        travelMapper.changeState(travel);
    }

    @Override
    public List<Travel> selectAll() {
        List<Travel> travels = travelMapper.selectAll();
        return travels;
    }

    @Override
    public List<Travel> queryStrategyTravel(StrategyTravelQuery qo) {
        List<Travel> travels=travelMapper.selectStrategyTravel(qo);
        return travels;
    }

    /**
     *  index查询游记的方法
     * @param qo 关键字为标题或者内容
     * @return
     */
    @Override
    public PageInfo<Travel> queryForTravel(QueryObject qo) {
        List<Travel> list=travelMapper.selectForStravel(qo);
        return new PageInfo<>(list);
    }

    /**
     * 游记文章的增加点赞方法
     */
    @Override
    public void updataLikeNum(Long id) {
        travelMapper.updateLikeNum(id);
    }

    /**
     *  游记取消赞的方法
     * @param relationId
     */
    @Override
    public void updateByDeleteLikeNum(Long relationId) {
        travelMapper.updateLikeNumByDelete(relationId);
    }

    public int getByUserId(Long userId) {
        return travelMapper.getByUserId(userId);
    }

    public void updateShowNum(Long travelId) {
        travelMapper.updateShowNum(travelId);
    }

}
